home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / DragView.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  6.6 KB  |  221 lines  |  [TEXT/CWIE]

  1. // DragView.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. /** Window subclass that implements "drag and drop" within the IFC library.
  10.   */
  11.  
  12.  
  13. class DragView extends InternalWindow {
  14.     int         animationCount, animationDeltaX, animationDeltaY;
  15.     boolean     _animatingBack, _wasAccepted;
  16.     Timer       timer;
  17.     DragSession session;
  18.  
  19.     /* constructors */
  20.  
  21.     /** Instantiates a DragView with origin (0, 0) and zero width and height.
  22.       * This constructor is only useful when decoding.
  23.       */
  24.     public DragView() {
  25.         super();
  26.     }
  27.  
  28.      /** Constructs the DragView to have bounds
  29.       * (<B>x</B>, <B>y</B>, <B>width</B>, <B>height</B>).
  30.       * (<b>mouseDownX</b>, <b>mouseDownY</b>) is the location of the mouse
  31.       * click that initiated the drag-and-drop session.  <B>dragSource</b> is
  32.       * the View that initiated the drag-and-drop session.  If not
  33.       * <b>null</b>, this object will receive a <b>dragComplete()</b> message
  34.       * once the drag-and-drop session has finished, and if <B>dragImage</b>
  35.       * is <b>null</b>, it will receive multiple
  36.       * <b>drawContentsOfDragView()</b>,
  37.       * asking the View to draw the DragView's contents.  <B>dragItem</b> is
  38.       * the data being dragged in the drag-and-drop session, and if
  39.       * <b>animateBack</b> is <b>true</b>, the DragView will animate back
  40.       * to its origin at the end of the drag session if the DragView's
  41.       * <B>dragItem</b> is not accepted.  <b>isTransparent</b> indicates
  42.       * whether the item being dragged should be placed within a
  43.       * transparent, rather than opaque, window.  All coordinates are
  44.       * expected to be absolute (that of the dragSource's rootView).
  45.       */
  46.     public DragView(DragSession session) {
  47.         super();
  48.  
  49.         setBounds(session.initialX, session.initialY,
  50.                   session.image.width(), session.image.height());
  51.  
  52.         this.session = session;
  53.         _lastX = session.mouseDownX;
  54.         _lastY = session.mouseDownY;
  55.  
  56.         setType(BLANK_TYPE);
  57.         setTransparent(session.image.isTransparent());
  58.         contentView().setTransparent(true);
  59.         setLayer(DRAG_LAYER);
  60.         setRootView(session.rootView);
  61.         show();
  62.         rootView().setMouseView(this);
  63.  
  64.         draw();
  65.     }
  66.  
  67.     /** DragViews receive the command "animateRejectedDrag" from a Timer while
  68.       * animating the slide back resulting from a rejected drag.
  69.       */
  70.     public void performCommand(String command, Object data) {
  71.         if ("animateRejectedDrag".equals(command))
  72.             animateRejectedDrag();
  73.         else
  74.             super.performCommand(command, data);
  75.     }
  76.  
  77.     private void animateRejectedDrag() {
  78.         int animX, animY;
  79.         MouseEvent dragEvent;
  80.  
  81.         // End the rejected drag animation when the count reaches 0.
  82.  
  83.         if (animationCount <= 0) {
  84.             timer.stop();
  85.             timer = null;
  86.             _animatingBack = false;
  87.             animationCount = 0;
  88.             animationDeltaX = 0;
  89.             animationDeltaY = 0;
  90.             stopDragging();
  91.  
  92.             return;
  93.         }
  94.  
  95.         // Compute the next location and send in a drag event.
  96.  
  97.         animationCount--;
  98.  
  99.         // The drag event needs to be in the relative coord system
  100.         animX = _lastX - (animationDeltaX + bounds.x);
  101.         animY = _lastY - (animationDeltaY + bounds.y);
  102.         dragEvent = new MouseEvent(0, MouseEvent.MOUSE_DRAGGED, animX, animY,
  103.                                    0);
  104.         mouseDragged(dragEvent);
  105.     }
  106.  
  107.     void startAnimatingRejectedDrag() {
  108.         float   deltaX, deltaY;
  109.         int     increment, count;
  110.  
  111.         _animatingBack = true;
  112.  
  113.         if (_lastX - session.mouseDownX != 0 &&
  114.             _lastY - session.mouseDownY != 0) {
  115.             deltaX = _lastX - session.mouseDownX;
  116.             deltaY = _lastY - session.mouseDownY;
  117.  
  118.             if (deltaX > deltaY) {
  119.                 increment = 1 + (int)(Math.abs(deltaY) / 5);
  120.                 count = (int)(Math.abs(deltaY) / increment);
  121.  
  122.                 deltaX /= Math.abs(deltaY);
  123.  
  124.                 if (deltaY < 0) {
  125.                     deltaY = -1.0F;
  126.                 } else {
  127.                     deltaY = 1.0F;
  128.                 }
  129.             } else if (deltaY > deltaX) {
  130.                 increment = 1 + (int)(Math.abs(deltaX) / 5);
  131.                 count = (int)(Math.abs(deltaX) / increment);
  132.  
  133.                 deltaY /= Math.abs(deltaX);
  134.  
  135.                 if (deltaX < 0) {
  136.                     deltaX = -1.0F;
  137.                 } else {
  138.                     deltaX = 1.0F;
  139.                 }
  140.             } else {
  141.                 count = 0;
  142.                 increment = 0;
  143.             }
  144.  
  145.             if (count > 0) {
  146.                 animationCount = count;
  147.                 animationDeltaX = (int)(increment * deltaX);
  148.                 animationDeltaY = (int)(increment * deltaY);
  149.  
  150.                 timer = new Timer(this, "animateRejectedDrag", 25);
  151.                 timer.start();
  152.             } else {
  153.                 _animatingBack = false;
  154.                 stopDragging();
  155.             }
  156.         } else {
  157.             _animatingBack = false;
  158.             stopDragging();
  159.         }
  160.     }
  161.  
  162.     /** Overridden to return <b>false</b>.
  163.       * @see InternalWindow#canBecomeMain
  164.       */
  165.     public boolean canBecomeMain() {
  166.         return false;
  167.     }
  168.  
  169.     /** Overridden to return <b>false</b>.
  170.       * @see InternalWindow#canBecomeDocument
  171.       */
  172.     public boolean canBecomeDocument() {
  173.         return false;
  174.     }
  175.  
  176.     /** Moves the DragView as the user moves the mouse.  Sends
  177.       * <B>dragViewEntered()</b>, <b>dragViewMoved</b>, and
  178.       * <B>dragViewExited</b> messages to
  179.       * interested Views as the mouse passes over them.
  180.       */
  181.     public void mouseDragged(MouseEvent event) {
  182.         super.mouseDragged(event);
  183.  
  184.         if (!_animatingBack) {
  185.             session.mouseDragged(event);
  186.         }
  187.     }
  188.  
  189.     void stopDragging() {
  190.         /* if we're already offscreen, we're done */
  191.         if (!isVisible()) {
  192.             return;
  193.         }
  194.  
  195.         hide();
  196.  
  197.         setBuffered(false);
  198.  
  199.         rootView.redraw(bounds);
  200.     }
  201.  
  202.     /** Ends the drag session by notifying the appropriate View that the
  203.       * DragView has been released over it.
  204.       */
  205.     public void mouseUp(MouseEvent event) {
  206.         super.mouseUp(event);
  207.         session.mouseUp(event);
  208.     }
  209.  
  210.     /** Draws the DragView's contents using its <B>dragImage</b>, or, if
  211.       * <B>null</b>, by asking its <B>dragSource</b> to do so.
  212.       */
  213.     public void drawView(Graphics g) {
  214.         session.image.drawAt(g, 0, 0);
  215.     }
  216.  
  217.     public View viewForMouse(int x, int y) {
  218.         return null;
  219.     }
  220. }
  221.